import random
import re

print("****************************************")
print("*               中国工商银行              *")
print("*               帐户管理系统              *")
print("*                 v1.0                 *")
print("****************************************")
print("*1.开户                                 *")
print("*2.存款                                 *")
print("*3.取款                                 *")
print("*4.转账                                 *")
print("*5.查询                                 *")
print("*6.退出                                 *")
print("****************************************")

user_git = {'12345678': {'name': '张三', 'password': '123456', 'addr': '北京市海淀区', 'money': 10000,
                         'bank_name': '中国工商银行'}}


def is_six_digit_num(num):
    pattern = r'^\d{6}$'
    return bool(re.match(pattern, num))


def create_bank_account(acount, name, password, addr, money, bank_name):
    if acount in user_git:
        return 2
    elif len(user_git) >= 100:
        return 3
    else:
        user_git[acount] = {"name": name, "password": password, "addr": addr,
                            "money": money, "bank_name": bank_name}
        return 1


def save_money(acount, money):
    if acount in user_git:
        user_git[acount]["money"] += money
        return True
    else:
        return False


def withdraw_money(acount, password, money):
    if acount in user_git:
        if password == user_git[acount]["password"]:
            if user_git[acount]["money"] >= money:
                user_git[acount]["money"] -= money
                return 0
            else:
                return 3
        else:
            return 2
    else:
        return 1


def transfer_money(account1, account2, password, money):
    if account1 in user_git and account2 in user_git:
        if password == user_git[account1]["password"]:
            if user_git[account1]["money"] >= money:
                user_git[account1]["money"] -= money
                user_git[account2]["money"] += money
                return 0
            else:
                return 3
        else:
            return 2
    else:
        return 1


def query_account(account, password):
    if account in user_git:
        if password == user_git[account]["password"]:
            print("姓名：", user_git[account]["name"])
            print("地址：", user_git[account]["addr"])
            print("余额：", user_git[account]["money"])
            print("银行名称：", user_git[account]["bank_name"])
        else:
            print("密码错误，请重新输入！")
    else:
        print("帐号不存在,请重新输入！")


while True:
    op = input("请输入您的操作编号：")
    if op == "1":
        account = ''.join(str(random.randint(0, 9)) for _ in range(8))
        name = input("请输入您的姓名：")
        password = input("请输入您的密码：")
        if is_six_digit_num(password):
            print("密码格式正确")
        else:
            print("密码格式错误，请重新输入！")
            continue
        addr = input("请输入您的地址：")
        money = 0
        bank_name = "中国工商银行"
        nub = create_bank_account(account, name, password, addr, money, bank_name)
        if nub == 2:
            print("帐号已存在，请重新输入！")
        elif nub == 3:
            print("帐户数量已达上限，请联系管理员！")
        elif nub == 1:
            print("您的帐户已开通，帐号为：", account)
    elif op == "2":
        account = input("请输入您的帐号：")
        money = int(input("请输入存款金额："))
        done = save_money(account, money)
        if done:
            print("存款成功！")
            print("您的余额为：", user_git[account]["money"])
        else:
            print("帐号不存在,请重新输入！")
            continue
    elif op == "3":
        account = input("请输入您的帐号：")
        password = input("请输入您的密码：")
        money = int(input("请输入取款金额："))
        nub = withdraw_money(account, password, money)
        if nub == 0:
            print("取款成功！")
            print("您的余额为：", user_git[account]["money"])
        elif nub == 1:
            print("帐号不存在,请重新输入！")
            continue
        elif nub == 2:
            print("密码错误，请重新输入！")
            continue
        elif nub == 3:
            print("余额不足，请重新输入！")
            continue
    elif op == "4":
        account1 = input("请输入转出帐号：")
        account2 = input("请输入转入帐号：")
        password = input("请输入您的密码：")
        money = int(input("请输入转账金额："))
        nub = transfer_money(account1, account2, password, money)
        if nub == 0:
            print("转账成功！")
            print("您的余额为：", user_git[account1]["money"])
            print("对方的余额为：", user_git[account2]["money"])
        elif nub == 1:
            print("帐号不存在,请重新输入！")
            continue
        elif nub == 2:
            print("密码错误，请重新输入！")
            continue
        elif nub == 3:
            print("余额不足，请重新输入！")
            continue
    elif op == "5":
        account = input("请输入您的帐号：")
        password = input("请输入您的密码：")
        query_account(account, password)
        print("查询成功！")
    elif op == "6":
        print("欢迎下次使用！")
        break
    else:
        print("输入错误，请重新输入！")
